﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class admin_qlproducttype : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99ffcc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("id");
        //Response.Write(id.Text);
        Response.Redirect("Default.aspx?page=edproducttype&id=" + id.Text);
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("insert into producttype values(@id, @logo, @cat, @des)", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter desc = new SqlParameter("@des", SqlDbType.NVarChar, 150);
        desc.Value = des.Text;

        SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
        Double idt = Common.ToSeconds(DateTime.Now);
        id.Value = idt;

        SqlParameter cat = new SqlParameter("@cat", SqlDbType.BigInt);
        cat.Value = protype.SelectedValue;

        SqlParameter logot = new SqlParameter("@logo", SqlDbType.NVarChar, 150);

        String regex = "";
        if (logo.PostedFile.ContentLength > 2097151)
        {
            info.Visible = true;
            info.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
            return;
        }
        else
        {
            regex = System.IO.Path.GetExtension(logo.PostedFile.FileName.ToLower().ToString());
            String picPath = "~/image/" + logo.PostedFile.FileName;
            if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
            {
                logo.SaveAs(Server.MapPath(picPath));
            }
            else
            {
                info.Visible = true;
                info.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
            }
        }
        if (regex == "")
        {
            logot.Value = null;
        }
        else
        {
            logot.Value = logo.PostedFile.FileName;
        }

        mySqlSelect.Parameters.Add(desc);
        mySqlSelect.Parameters.Add(id);
        mySqlSelect.Parameters.Add(cat);
        mySqlSelect.Parameters.Add(logot);
        mySqlSelect.ExecuteNonQuery();

        cn.Close();
        SqlDataSource1.DataBind();
        GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.RowIndex].Cells[0].FindControl("id");
        connection con = new connection();
        SqlConnection cn = con.connect();
        //delete du lieu o bang producttype_lang
        SqlCommand mySqlSelect = new SqlCommand("delete from producttype_lang where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter idt = new SqlParameter("@id", SqlDbType.BigInt);
        idt.Value = Int64.Parse(id.Text);

        mySqlSelect.Parameters.Add(idt);
        mySqlSelect.ExecuteNonQuery();
        //delete du lieu o bang producttype
        SqlCommand mySqlSelect1 = new SqlCommand("delete from producttype where id = @idtt", cn);
        mySqlSelect1.CommandType = CommandType.Text;

        SqlParameter idtt = new SqlParameter("@idtt", SqlDbType.BigInt);
        idtt.Value = Int64.Parse(id.Text);

        mySqlSelect1.Parameters.Add(idtt);
        mySqlSelect1.ExecuteNonQuery();

        cn.Close();
        SqlDataSource1.DataBind();
        GridView1.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.CompareTo("addlang") == 0)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            Label id = (Label)row.FindControl("id");
            Response.Redirect("Default.aspx?page=qlprotylang&id=" + id.Text);
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}